package com.wang.experiment.experiment3.horseracing;

import javax.swing.*;
import java.awt.*;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.PipedInputStream;
import java.nio.channels.Pipe;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;

/**
 * @author wr
 * @date 2021/10/27 7:57
 */
public class HorseRacing extends JFrame {
    private final int HORSE_NUM = 14;
    private final String[] HORSE_NAME = {
            "  特别周", "无声铃鹿", "东海帝皇", "丸善斯基", "富士奇石",
            "  小栗帽", "  黄金船", "  伏特加", "大和赤骥", "大树快车",
            "  草上飞", "    皇帝", "    米浴", "川上公主"};

    public HorseRacing(String title) throws HeadlessException {
        super(title);
        init();
    }

    public void init() {
        setSize(1080, 720);
        setResizable(false);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(screenSize.width / 2 - 1080 / 2, screenSize.height / 2 - 720 / 2, 1080, 720);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        //init header
        JPanel header = new JPanel(new FlowLayout());
        JLabel title = new JLabel("赛马");
        title.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 30));
        header.add(title);
        add(header, BorderLayout.NORTH);

        //init main
        List<JProgressBar> progressBars = new ArrayList<>();
        List<JLabel> ranks = new ArrayList<>();
        JPanel main = new JPanel(new GridLayout(14, 1));
        for (int i = 0; i < HORSE_NUM; i++) {
            JPanel panel = new JPanel(new FlowLayout());
            JLabel name = new JLabel(HORSE_NAME[i]);
            panel.add(name);
            JProgressBar progressBar = new JProgressBar(0, 10000);
            progressBar.setPreferredSize(new Dimension(800, 20));
            progressBars.add(progressBar);
            panel.add(progressBar);
            JLabel rank = new JLabel();
            ranks.add(rank);
            panel.add(rank);
            main.add(panel);
        }
        add(main, BorderLayout.CENTER);

        //init footer
        JPanel footer = new JPanel(new FlowLayout());
        JButton start = new JButton("开始");
        JButton reset = new JButton("重置");
        footer.add(start);
        footer.add(reset);
        add(footer, BorderLayout.SOUTH);

        RaceService raceService = new RaceService(HORSE_NUM);
        start.addActionListener(e -> {
            Rank rank = new Rank();
            Random random = new Random(System.currentTimeMillis());
            for (int i = 0; i < HORSE_NUM; i++) {
                Horse horse = new Horse(progressBars.get(i), ranks.get(i), rank);
                //设置不同的线程优先级
                horse.setPriority(random.nextInt(9) + 1);
                raceService.getServiceInstance().execute(horse);
            }
            //等待线程池中任务完成后终止
            raceService.getServiceInstance().shutdown();
        });
        reset.addActionListener(e -> {
            //立刻终止线程池
            raceService.getServiceInstance().shutdownNow();
            for(JProgressBar b : progressBars){
                b.setValue(b.getMinimum());
            }
            for(JLabel l : ranks){
                l.setText("");
            }
        });

        setVisible(true);
    }

    public static void main(String[] args) {
        HorseRacing horseRacing = new HorseRacing("赛马");
        horseRacing.init();
    }
}
